<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-6
 * Time: 下午4:41
 */
class Marticle extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    function getArticleAll($column_id,$pageSize,$limit=0,$order='a.`id` desc',$field='a.`id`,a.`title`,a.`column_id`,a.`author`,a.`create_time`,a.`url`,au.`uid`')
    {

        if(!empty($column_id)){
            $where = 'a.`column_id`='.$column_id;
        }

        if($limit > 0){
            $limit = "limit 0,$limit";
            $sql = "SELECT $field from `tbarticle` as a join `tbarticle_user` as au on a.id=au.article_id  where {$where} order by $order $limit ";
            $query = $this->db->query($sql);
            $data['data'] = $query->result_array();
        }else{
            $countQuery = $this->db->query("SELECT a.id from `tbarticle` as a join `tbarticle_user` as au on a.id=au.article_id where $where ");
            $count = $countQuery->num_rows() ;
            $sql = "SELECT $field from `tbarticle` as a join `tbarticle_user` as au on a.id=au.article_id where {$where} order by $order limit ";

            $data = $this->db->pager($count,$pageSize,$sql);
        }

        return $data;
    }

    function getArticleIntro($column_id=7,$limit,$order,$field)
    {

        $where = '`column_id`='.$column_id;

        $limit = "limit 0,$limit";
        $sql = "SELECT $field from `tbarticle` where {$where} order by $order $limit ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    function getArticleById($id)
    {

        $sql = "select * from `tbarticle` where `id`=?";
        $query = $this->db->query($sql,array($id));
        $row = $query->row_array();

        //文章对应的用户
        if(!empty($row)){

            $sqlArticle = "select * from `tbarticle_user` where `article_id`=".$row['id'];
            $queryArticle = $this->db->query($sqlArticle);

            $item = array();
            foreach($queryArticle->result() as $value){
                $item[] = $value->uid;
            }
            $row['uids'] = implode(',',$item);
        }
        return $row;
    }

    function getArticleByColumnId($id)
    {

        $sql = "select * from `tbarticle` where `column_id`=?";
        $query = $this->db->query($sql,array($id));
        $row = $query->row_array();

        //文章对应的用户
        if(!empty($row)){

            $sqlArticle = "select * from `tbarticle_user` where `article_id`=".$row['id'];
            $queryArticle = $this->db->query($sqlArticle);

            $item = array();
            foreach($queryArticle->result() as $value){
                $item[] = $value->uid;
            }

            $row['uids'] = implode(',',$item);
        }

        return $row;
    }

    /**
     * 获取用户一条微访谈
     * @param $uid
     * @param $field
     * @return mixed
     */
    function getArticleByUid($uid)
    {
        $sql = "select * from `tbarticle_user` as tau left join `tbarticle` as ta on ta.id=tau.article_id where tau.`uid` = ? order by tau.id desc limit 1";
        $query = $this->db->query($sql,array($uid));
        $data = $query->result_array();

        return $data;
    }


}